

package com.zdsx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zdsx.common.exception.CommonException;
import com.zdsx.common.exception.ErrorCode;
import com.zdsx.common.service.impl.BaseServiceImpl;
import com.zdsx.common.utils.JsonUtils;
import com.zdsx.dao.SysParamsDao;
import com.zdsx.entity.SysParamsEntity;
import com.zdsx.service.SysParamsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 参数管理
 *
 * @author Lr
 * @since 1.0.0
 */
@Service
@AllArgsConstructor
public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsDao, SysParamsEntity> implements SysParamsService {

    @Override
    public String getValue(String paramCode) {
//        String paramValue = sysParamsRedis.get(paramCode);
//        if (paramValue == null) {
//            paramValue = baseDao.getValueByCode(paramCode);
//
//            sysParamsRedis.set(paramCode, paramValue);
//        }
//        return paramValue;
        return baseDao.getValueByCode(paramCode);
    }

    @Override
    public <T> T getValueObject(String paramCode, Class<T> clazz) {
        String paramValue = getValue(paramCode);
        if (StrUtil.isNotBlank(paramValue)) {
            return JsonUtils.parseObject(paramValue, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new CommonException(ErrorCode.PARAMS_GET_ERROR);
        }
    }

}